package com.sohoo.ftp;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.InetAddress;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;

public class FtpClientDemo2 {
		public static void main(String[] args) throws Exception {
			//Ftp服务器主要完成 文件的上传
			//文件下载
			FTPClient ftpClient = new FTPClient();
			FTPClientConfig config = new FTPClientConfig();
			InetAddress host = InetAddress.getByName("192.168.1.249");
			InputStream inputStream = FtpClientDemo2.class.getClassLoader().getResourceAsStream("test.jpg");
			int port = 2121;
			ftpClient.connect(host, port);
			String username="test";
			String password="test";
			boolean login = ftpClient.login(username, password);
			if(login) System.out.println("Ftp服务器登录成功");
			String printWorkingDirectory = ftpClient.printWorkingDirectory();
			System.out.println("当前工作目录为"+printWorkingDirectory);
			String remote = "sdfsdfsdfsdf.jpg";
			ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//要设置文件类型
//			boolean storeFile = ftpClient.storeFile(remote, inputStream);
			InputStream fileStream = ftpClient.retrieveFileStream(remote);
			FileOutputStream outputStream = new FileOutputStream("D:/meinv.jpg");
			IOUtils.copy(fileStream, outputStream);
			ftpClient.disconnect();
		}
}
